<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        新增媒体服务器
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-curent-row
    >
      <el-table-column align="center" label="序号" min-width="5%">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="mediaServerId" align="center" min-width="19%">
        <template slot-scope="scope">
          <span>{{ scope.row.server_id_zlk }}</span>
        </template>
      </el-table-column>

      <el-table-column label="外网IP" align="center" min-width="19%">
        <template slot-scope="scope">
          <span>{{ scope.row.media_ip }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内网IP" align="center" min-width="19%">
        <template slot-scope="scope">
          <span>{{ scope.row.intranet }}</span>
        </template>
      </el-table-column>
      <el-table-column label="http 端口" align="center" min-width="19%">
        <template slot-scope="scope">
          <span>{{ scope.row.http_port }}</span>
        </template>
      </el-table-column>
      <el-table-column label="https 端口" align="center" min-width="19%">
        <template slot-scope="scope">
          <span>{{ scope.row.https_port }}</span>
        </template>
      </el-table-column>
      <el-table-column label="rtmp 端口" align="center" min-width="19%">
        <template slot-scope="scope">
          <span>{{ scope.row.rtmp_port }}</span>
        </template>
      </el-table-column>
      <el-table-column label="rtp 端口" align="center" min-width="19%">
        <template slot-scope="scope">
          <span>{{ scope.row.rtp_min_port }} - {{ scope.row.rtp_max_port }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="210" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleView(row)">
            查看
          </el-button>
          <el-button type="primary" size="mini" @click="handleEdit(row)">
            编辑
          </el-button>
          <el-button v-if="row.status!=='deleted'" size="mini" type="danger" @click="handleDelete(row,$index)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <mediaServerEdit ref="mediaServerEdit" :show.sync="dialogEditVisible" :dialog-status="dialogStatus" :edit-item="editItem" />
    <mediaServerView ref="mediaServerView" :show.sync="dialogViewVisible" :view-item="viewItem" />
  </div>
</template>

<script>
import { list, del } from '@/api/mediaServer'
import mediaServerEdit from '@/views/setting/mediaServerEdit.vue'
import mediaServerView from '@/views/setting/mediaServerView.vue'

export default {
  // 引用组件 view edit
  components: {
    mediaServerEdit,
    mediaServerView
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      pagesize: 10,
      page: 1,
      listLoading: true,
      dialogEditVisible: false,
      dialogViewVisible: false,
      dialogStatus: '',
      editItem: {},
      viewItem: {}
    }
  },

  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      const data = { page: this.page, pagesize: this.pagesize }
      list(data).then(response => {
        this.list = response.data.servers
        this.list.forEach((item) => {
          item.edit = false
        })
        this.listLoading = false
        this.total = response.data.total
      })
    },
    current_change: function(page) {
      this.page = page
      this.fetchData()
    },
    handleCreate() {
      const temp = {
        server_id: '',
        media_ip: '',
        intranet: '',
        http_port: '',
        rtmp_port: '',
        rtp_min_port: '',
        rtp_max_port: ''
      }
      this.dialogStatus = 'create'
      this.dialogEditVisible = true
      this.editItem = temp
    },
    handleEdit(row) {
      const temp = Object.assign({}, row) // copy obj
      console.info('handleEdit', this.temp)
      // secret 被用做登陆校验，这里form要改个名字
      temp.zl_secret = temp.secret
      delete temp.secret
      this.dialogStatus = 'update'
      this.dialogEditVisible = true
      this.editItem = temp
    },
    handleView(row) {
      const temp = Object.assign({}, row) // copy obj
      this.dialogViewVisible = true
      this.viewItem = temp
    },
    handleDelete(row) {
      this.$confirm('确认删除此媒体服务器信息？').then(_ => {
        this.listLoading = true
        del({ id: row.id }).then(() => {
          this.listLoading = false
          this.fetchData()
        })
      }).catch(_ => {

      })
    }
  }
}
</script>

